import { Container } from "pixi.js";
import { Scope } from "../../../typings/traverse";
import { ResourceVal } from "../../../typings/utils";
import GlobalResource from '../../ResourceGlobal';
import {
  absolutePath,
  checkNamespace,
  getGoalData,
  parseResourceVal,
  preResAttributeLoad,
} from "../../../utils";
import Resource from "../../ResourceCenter";
interface DealGoal {
  $data: Record<string, any>;
  $watcher: Record<string, Record<string, Array<Function>>>;
  $resource: Resource | undefined;
  $globalResource?: GlobalResource;
}
const ResourceType = ["sprite", "animation"];
export function v_resource(
  goal: DealGoal,
  container: Container,
  element: Element,
  scope: Scope | undefined
) {
  ResourceType.forEach((item) => {
    const depName = "data";
    const { $data, $watcher, $resource, $globalResource } = goal;
    if (element.hasAttribute(`v-resource.${item}`)) {
      let _v_resource = <string>element.getAttribute(`v-resource.${item}`);
      let path = preResAttributeLoad(_v_resource);
      if (scope) {
        const { item, items, key } = scope;
        let isTextNamespace = checkNamespace(path, item);
        if (isTextNamespace) {
          path = absolutePath(items, item, path);
        }
      }
      let v_resource = preResAttributeLoad(getGoalData($data, path));
      let checked: undefined | number;
      if (!$watcher[depName][path]) {
        $watcher[depName][path] = [];
      }
      if (item == "sprite") {
        let empty = new PIXI.Sprite(PIXI.Texture.EMPTY);
        container.addChildAt(empty, 0);
        if ($watcher[path]) {
          $watcher[depName][path] = [];
        }
        let watcher = () => {
          let val: any;
          let partCb = () => {
            if (Number.isInteger(Number(checked))) {
              if (Array.isArray($resource!.$sprite[val])) {
                empty.texture = (<Array<PIXI.Texture>>(
                  $resource!.$sprite[val]
                ))[Number(checked)];
              }
            } else {
              if (!Array.isArray($resource!.$sprite[val])) {
                empty.texture = <PIXI.Texture>$resource!.$sprite[val];
              }
            }
          };
          return (_val: string) => {
            if ($resource) {
              $resource!.removeLoadlistener(val, partCb);
            }
            let resourceVal: ResourceVal = parseResourceVal(_val);
            val = resourceVal.val;
            checked = resourceVal.checked;
            if ($resource) {
              if ($resource._config.sprite) {
                if ($resource._config.sprite[val]) {
                  if ($resource.$sprite[val]) {
                    if (Number.isInteger(Number(checked))) {
                      if (Array.isArray($resource!.$sprite[val])) {
                        empty.texture = (<Array<PIXI.Texture>>(
                          $resource!.$sprite[val]
                        ))[Number(checked)];
                      }
                    } else {
                      if (!Array.isArray($resource!.$sprite[val])) {
                        empty.texture = <PIXI.Texture>(
                          $resource!.$sprite[val]
                        );
                      }
                    }
                  } else {
                    $resource.addLoadListener(val, partCb);
                  }
                } else {
                  if ($globalResource) {
                    if ($globalResource.$sprite) {
                      if ($globalResource.$sprite[val]) {
                        if (Number.isInteger(Number(checked))) {
                          if (
                            Array.isArray($globalResource!.$sprite[val])
                          ) {
                            empty.texture = (<Array<PIXI.Texture>>(
                              $globalResource!.$sprite[val]
                            ))[Number(checked)];
                          }
                        } else {
                          if (
                            !Array.isArray($globalResource!.$sprite[val])
                          ) {
                            empty.texture = <PIXI.Texture>(
                              $globalResource!.$sprite[val]
                            );
                          }
                        }
                      }
                    }
                  }
                }
              } else {
                if ($globalResource) {
                  if ($globalResource.$sprite) {
                    if ($globalResource.$sprite[val]) {
                      if (Number.isInteger(Number(checked))) {
                        if (Array.isArray($globalResource!.$sprite[val])) {
                          empty.texture = (<Array<PIXI.Texture>>(
                            $globalResource!.$sprite[val]
                          ))[Number(checked)];
                        }
                      } else {
                        if (
                          !Array.isArray($globalResource!.$sprite[val])
                        ) {
                          empty.texture = <PIXI.Texture>(
                            $globalResource!.$sprite[val]
                          );
                        }
                      }
                    }
                  }
                }
              }
            } else {
              if ($globalResource) {
                if ($globalResource.$sprite) {
                  if ($globalResource.$sprite[val]) {
                    if (Number.isInteger(Number(checked))) {
                      if (Array.isArray($globalResource.$sprite[val])) {
                        empty.texture = (<Array<PIXI.Texture>>(
                          $globalResource!.$sprite[val]
                        ))[Number(checked)];
                      }
                    } else {
                      if (!Array.isArray($globalResource.$sprite[val])) {
                        empty.texture = <PIXI.Texture>(
                          $globalResource!.$sprite[val]
                        );
                      }
                    }
                  }
                }
              }
            }
          };
        };
        watcher()(v_resource);
        $watcher[depName][path].push(watcher());
      } else if (item == "animation") {
        let empty = new PIXI.AnimatedSprite([PIXI.Texture.EMPTY]);
        container.addChildAt(empty, 0);
        if (!$watcher[path]) {
          $watcher[depName][path] = [];
        }
        let watcher = () => {
          let val: any;
          let partCb = () => {
            empty.textures = $resource!.$animatedSprite[val];
          };
          return (_val: any) => {
            if ($resource) {
              $resource!.removeLoadlistener(val, partCb);
            }
            let resourceVal: ResourceVal = parseResourceVal(_val);
            val = resourceVal.val;
            checked = resourceVal.checked;
            if ($resource) {
              if ($resource._config.animatedSprite) {
                if ($resource._config.animatedSprite[val]) {
                  if ($resource.$animatedSprite[val]) {
                    empty.textures = $resource.$animatedSprite[val];
                  } else {
                    $resource.addLoadListener(val, partCb);
                  }
                } else {
                  if ($globalResource) {
                    if ($globalResource.$animatedSprite) {
                      if ($globalResource.$animatedSprite[val]) {
                        empty.textures = $globalResource.$animatedSprite[
                          val
                        ];
                      }
                    }
                  }
                }
              } else {
                if ($globalResource) {
                  if ($globalResource.$animatedSprite) {
                    if ($globalResource.$animatedSprite[val]) {
                      empty.textures = $globalResource.$animatedSprite[
                        val
                      ];
                    }
                  }
                }
              }
            } else {
              if ($globalResource) {
                if ($globalResource.$animatedSprite) {
                  if ($globalResource.$animatedSprite[val]) {
                    empty.textures = $globalResource.$animatedSprite[val];
                  }
                }
              }
            }
          };
        };
        watcher()(v_resource);
        $watcher[depName][path].push(watcher());
      }
    }
  });
}
